//
//  TileMenuView.m
//  SAYAKA_Table
//
//  Created by numanuma08 on 2013/07/20.
//  Copyright (c) 2013年 勘米良. All rights reserved.
//

#define MAIN_SCROLLVIEW_TAG 1000

#import <QuartzCore/QuartzCore.h>
#import "CategoryManager.h"
#import "ItemManager.h"
#import "AppDelegate.h"
#import "TileMenuView.h"
#import "Item.h"

@implementation TileMenuView
@synthesize categoryItems = _categoryItems;
@synthesize delegate;
@synthesize tileMenuButton1 = _tileMenuButton1;
@synthesize tileMenuButton2 = _tileMenuButton2;
@synthesize tileMenuButton3 = _tileMenuButton3;
@synthesize tileMenuButton4 = _tileMenuButton4;
@synthesize tileMenuButton5 = _tileMenuButton5;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

+ (id)tileMenuView
{
    UINib *nib = [UINib nibWithNibName:@"TileMenuView" bundle:nil];
    ItemThumbnailView *view = [[nib instantiateWithOwner:self options:nil] objectAtIndex:0];
    return view;
}

- (void)update
{
    [self initializeLayout];
    [self initializeAllTileMenuButton];
    [self addItemImage];
    TileMenuPageView *tileMenuPageView = (TileMenuPageView*)[_mainScrollView viewWithTag:MAIN_SCROLLVIEW_TAG+0];
    [tileMenuPageView startAutoScrollAllThumnbnail];
}

- (void)reload
{
    [self initializeLayout];
    [self addItemImage];
    TileMenuPageView *tileMenuPageView = (TileMenuPageView*)[_mainScrollView viewWithTag:MAIN_SCROLLVIEW_TAG+0];
    [tileMenuPageView startAutoScrollAllThumnbnail];
}

- (void)initializeLayout
{
    _categoryItems = nil;
    _categoryItems = [[NSMutableArray alloc] init];
    for (UIView *view in _mainScrollView.subviews) {
        [view removeFromSuperview];
    }
    
    AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
    NSArray *categorys = [CategoryManager allRecord:appDelegate.managedObjectContext];
    for (Category *category in categorys) {
        NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
        [dic setValue:category forKey:@"category"];

        NSArray *items = [ItemManager record:appDelegate.managedObjectContext byCategory:category];
        [dic setValue:items forKey:@"item"];
        
        [_categoryItems addObject:dic];
    }
    
    NSInteger page = 0;
    for (NSDictionary *categoryItem in _categoryItems) {
        NSArray *items = [categoryItem valueForKey:@"item"];
        NSInteger categoryPage = [items count] / 7;
        categoryPage++;
        page += categoryPage;
    }
    
    _mainScrollView.contentSize = CGSizeMake(_mainScrollView.frame.size.width*page,
                                             _mainScrollView.frame.size.height);
    _mainScrollView.contentOffset = CGPointMake(0, 0);
    
    _categoryScrollView.layer.shadowColor = [[UIColor blackColor] CGColor];
    _categoryScrollView.layer.shadowOpacity = 0.5;
    _categoryScrollView.layer.shadowOffset = CGSizeMake(2.5, 2.5);
    
    _currentPage = 0;
    _maxPage = page;
    


    
//    for (int cnt = 1; cnt <= 5; cnt++) {
//        Category *category
//        [self initializeTileMenuButtons:cnt];
//    }
//    _tileMenuButton1 = [TileMenuButton tileMenuButton];
//    [_tileMenuButtonArea1 addSubview:_tileMenuButton1];
//    _tileMenuButton2 = [TileMenuButton tileMenuButton];
//    [_tileMenuButtonArea2 addSubview:_tileMenuButton2];
//    _tileMenuButton3 = [TileMenuButton tileMenuButton];
//    [_tileMenuButtonArea3 addSubview:_tileMenuButton3];
//    _tileMenuButton4 = [TileMenuButton tileMenuButton];
//    [_tileMenuButtonArea4 addSubview:_tileMenuButton4];
//    _tileMenuButton5 = [TileMenuButton tileMenuButton];
//    [_tileMenuButtonArea5 addSubview:_tileMenuButton5];
}

- (void)initializeAllTileMenuButton
{
    for (int cnt = 0; cnt < [_categoryItems count]; cnt++) {
        NSDictionary *dic = [_categoryItems objectAtIndex:cnt];
        Category *category = [dic valueForKey:@"category"];
        NSLog(@">>%@", category.name);
        [self initializeTileMenuButton:category number:cnt+1];
    }
}

- (void)initializeTileMenuButton:(Category *)category number:(NSInteger)number
{
    UIView *area;
    TileMenuButton *tileMenuButton;
    switch (number) {
        case 1:
            area = _tileMenuButtonArea1;
            _tileMenuButton1 = [TileMenuButton tileMenuButton];
            tileMenuButton = _tileMenuButton1;
            [tileMenuButton setSelected:TRUE];
            break;
        case 2:
            area = _tileMenuButtonArea2;
            _tileMenuButton2 = [TileMenuButton tileMenuButton];
            tileMenuButton = _tileMenuButton2;
            break;
        case 3:
            area = _tileMenuButtonArea3;
            _tileMenuButton3 = [TileMenuButton tileMenuButton];
            tileMenuButton = _tileMenuButton3;
            break;
        case 4:
            area = _tileMenuButtonArea4;
            _tileMenuButton4 = [TileMenuButton tileMenuButton];
            tileMenuButton = _tileMenuButton4;
            break;
        case 5:
            area = _tileMenuButtonArea5;
            _tileMenuButton5 = [TileMenuButton tileMenuButton];
            tileMenuButton = _tileMenuButton5;
            break;
        default:
            return;
    }
    
    //tileMenuButton = [TileMenuButton tileMenuButton];
    tileMenuButton.category = category;
    tileMenuButton.delegate = self;
    [area addSubview:tileMenuButton];
    tileMenuButton.layer.shadowColor = [[UIColor blackColor] CGColor];
    tileMenuButton.layer.shadowOpacity = 0.3;
    tileMenuButton.layer.shadowOffset = CGSizeMake(2.5, 2.5);
}

- (void)addItemImage;
{
    NSInteger currentPage = 0;
    for (NSDictionary *categoryItem in _categoryItems) {
        Category *category = [categoryItem valueForKey:@"category"];
        TileMenuPageView *menuPageView = [TileMenuPageView tileMenuPageView];
        menuPageView.delegate = self;
        menuPageView.category = category;
        menuPageView.frame = CGRectMake(_mainScrollView.frame.size.width*currentPage, 0,
                                        menuPageView.frame.size.width, menuPageView.frame.size.height);
        [menuPageView setTag:MAIN_SCROLLVIEW_TAG+currentPage];
        [_mainScrollView addSubview:menuPageView];
        
        NSArray *items = [categoryItem valueForKey:@"item"];
        for (int cnt = 0; cnt < [items count]; cnt++) {
            if (6 < cnt+1 && (cnt+1)%6 == 1) {
                currentPage++;
                menuPageView = [TileMenuPageView tileMenuPageView];
                menuPageView.delegate = self;
                menuPageView.category = category;
                menuPageView.frame = CGRectMake(_mainScrollView.frame.size.width*currentPage, 0,
                                                menuPageView.frame.size.width, menuPageView.frame.size.height);
                [menuPageView setTag:MAIN_SCROLLVIEW_TAG+currentPage];
                [_mainScrollView addSubview:menuPageView];
            }
            
            Item *item = [items objectAtIndex:cnt];
            [menuPageView setItemThumbnail:item number:cnt%6+1];

        }
        currentPage++;
    }
}

- (void)deselectedAllTileMenuButton
{
    [_tileMenuButton1 setSelected:FALSE];
    [_tileMenuButton2 setSelected:FALSE];
    [_tileMenuButton3 setSelected:FALSE];
    [_tileMenuButton4 setSelected:FALSE];
    [_tileMenuButton5 setSelected:FALSE];
}

- (void)selectedTileMenuButtonByCategory:(Category *)category
{
    [self deselectedAllTileMenuButton];
    
    if ([_tileMenuButton1.category.category_id isEqualToNumber:category.category_id])
        [_tileMenuButton1 setSelected:TRUE];
    if ([_tileMenuButton2.category.category_id isEqualToNumber:category.category_id])
        [_tileMenuButton2 setSelected:TRUE];
    if ([_tileMenuButton3.category.category_id isEqualToNumber:category.category_id])
        [_tileMenuButton3 setSelected:TRUE];
    if ([_tileMenuButton4.category.category_id isEqualToNumber:category.category_id])
        [_tileMenuButton4 setSelected:TRUE];
    if ([_tileMenuButton5.category.category_id isEqualToNumber:category.category_id])
        [_tileMenuButton5 setSelected:TRUE];
}

- (void)jumpToCategoryTop:(Category *)jumpCategory
{
    NSInteger page = 0;
    for (NSDictionary *categoryItem in _categoryItems) {
        Category *category = [categoryItem valueForKey:@"category"];
        
        if ([category.category_id isEqualToNumber:jumpCategory.category_id]) {
            _mainScrollView.contentOffset = CGPointMake(_mainScrollView.frame.size.width*page,
                                                        _mainScrollView.contentOffset.y);
            return;
        } else {
            NSArray *items = [categoryItem valueForKey:@"item"];
            NSInteger categoryPage = [items count] / 7;
            categoryPage++;
            page += categoryPage;
        }
    }
}

- (void)tileMenuPageView:(TileMenuPageView *)tileMenuPageView didSelectItem:(Item *)item
{
    if ([self.delegate respondsToSelector:@selector(tileMenuView:didSelectItem:)]) {
        [self.delegate tileMenuView:self didSelectItem:item];
    }
}

- (void)tileManuButtonDidTouchUpInside:(TileMenuButton *)tileManuButton
{
    [self deselectedAllTileMenuButton];
    [tileManuButton setSelected:TRUE];
    [self jumpToCategoryTop:tileManuButton.category];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat scrollViewWidth = _mainScrollView.frame.size.width;
    NSInteger page = floor((_mainScrollView.contentOffset.x - scrollViewWidth / 2) / scrollViewWidth) +1;
    if (0 <= page && page <= _maxPage && page != _currentPage) {
        TileMenuPageView *tileMenuPageView = (TileMenuPageView*)[_mainScrollView viewWithTag:MAIN_SCROLLVIEW_TAG+page];
        [tileMenuPageView startAutoScrollAllThumnbnail];
        [self selectedTileMenuButtonByCategory:tileMenuPageView.category];
        _currentPage = page;
    }
}
@end
